package com.sunwayworld.basemodule.samplemanage.samplehandle.validators;

import com.sunwayworld.basemodule.samplemanage.samplehandle.bean.LimsSampleHandleBean;
import com.sunwayworld.basemodule.samplemanage.samplehandle.service.LimsSampleHandleService;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsSampleHandleSubmitValidator implements DataValidator {

    @Autowired
    private LimsSampleHandleService sampleHandleService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        List<LimsSampleHandleBean> handleBeans = wrapper.parse(LimsSampleHandleBean.class);
        List<Long> ids = handleBeans.stream().map(LimsSampleHandleBean::getId).collect(Collectors.toList());
        List<LimsSampleHandleBean> sampleHandleBeans = sampleHandleService.selectListByFilter(SearchFilter.instance().match("id", ids).filter(MatchPattern.OR));

        if(sampleHandleBeans.stream().anyMatch(s -> s.getSampleNum() == 0)){
            this.addConstraintViolation(I18nHelper.getMessage("T_LIMS_BATCH_SAMPLE.NOT.EMPTY"));
            return false;
        }
        return true;
    }
}
